import java.util.ArrayList;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;

public class StreamTest {



//    public static void main(String[] args) {
//        LinkedList<Integer> integers = new LinkedList<>();
//        integers.add(1);
//        integers.add(1);
//        integers.add(1);
//        integers.add(1);
//        integers.add(1);
//        System.out.println(integers.stream().count());
////        Object[] objects = integers.stream().toArray();
//
////        // 1.定义一个集合
////        ArrayList<String> list = new ArrayList<>();
////        list.add("张三");
////        list.add("张四");
////        list.add("四");
////        list.add("张四");
////        list.add("张四");
////        list.add("张四");
////        list.add("张四");
////        // 2.挑选list中包含张三的
////        ArrayList<String> list1 = new ArrayList<>();
////        for (String s : list) {
////            if (s.startsWith("张三")) {
////                list1.add(s);
////            }
////        }
////        System.out.println("挑选list包含张三的结果：" + list1);
////        // 3.再从list1中挑选挑选名字长度为2的
////        ArrayList<String> list2 = new ArrayList<>();
////        for (String s : list1) {
////            if (s.length() == 2) {
////                list2.add(s);
////            }
////        }
////        System.out.println("挑选list1长度为2的结果：" + list2);
////        // 4、遍历
////        for (String s : list2) {
////            System.out.println("最终结果 = " + s);
////        }
//
//
//        // 定义一个集合
////        ArrayList<String> list = new ArrayList<>();
////        list.add("1");
////        list.add("2");
////        list.add("李四");
////        list.add("李四");
////        list.add("李四");
////        list.add("李四");
////        list.add("李四");
//
//        //Stream流实现
////        list.stream()
////                .filter((name) -> name.startsWith("张三"))
////                .filter((name) -> name.length()==2)
////                .forEach((name) -> System.out.println(name));
////
//
////        list.stream().forEach((aa)-> System.out.println(aa));
////        list.stream().filter((name)->name.length()==3).forEach((name)-> System.out.println(name));
//////TODO:2024/1/9 不知道怎么用呢
////        list.stream().map(num->Integer.parseInt(num));
//
//
//
//    }


    static   class User {
        private String name;
        //age
        private int age;
        //分数
        private double fraction;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }

        public double getFraction() {
            return fraction;
        }

        public void setFraction(double fraction) {
            this.fraction = fraction;
        }
        public User(String name, int age, double fraction) {
            this.name = name;
            this.age = age;
            this.fraction = fraction;
        }
    }

    public static void main(String[] args) {
        List<User> userList = new ArrayList<>();
        userList.add(new User("a1", 22, 2.2));
        userList.add(new User("a2", 22, 2.5));
        userList.add(new User("a3", 40, 2.7));
        userList.add(new User("a4", 45, 2.8));

        //返回 对象集合以UsergetAge升序排序:年龄   --默认升序
        userList.stream().sorted(Comparator.comparing(User::getAge));

        //返回 对象集合以UsergetAge降序排序  ===这里写在前面 和写在后面要看清楚，弄明白
        userList.stream().sorted(Comparator.comparing(User::getAge).reversed()); //排序结果后再排序,
        userList.stream().sorted(Comparator.comparing(User::getAge, Comparator.reverseOrder()));//是直接进行排序

        //返回 对象集合以UsergetAge升序排序:**年龄并返回前n个元素**  --默认升序  ==下面同样写法
        userList.stream().sorted(Comparator.comparing(User::getAge)).limit(2);

        //返回 对象集合以UsergetAge升序排序:**年龄并去除前 n 个元素**  --默认升序 ==下面同样写法
        userList.stream().sorted(Comparator.comparing(User::getAge)).skip(2);

        //返回 对象集合以UsergetAge升序 getFraction升序
        userList.stream().sorted(Comparator.comparing(User::getAge).thenComparing(User::getFraction));

        //先以getAge升序,升序结果进行getAge降序,再进行getFraction升序
        userList.stream().sorted(Comparator.comparing(User::getAge).reversed().thenComparing(User::getFraction));

        //先以getAge降序,再进行getFraction升序
        userList.stream().sorted(Comparator.comparing(User::getAge, Comparator.reverseOrder()).thenComparing(User::getFraction));

        //先以getAge升序,升序结果进行getAge降序,再进行getFraction降序
        userList.stream().sorted(Comparator.comparing(User::getAge).reversed().thenComparing(User::getFraction, Comparator.reverseOrder()));

        //先以getAge降序,再进行getFraction降序
        userList.stream().sorted(Comparator.comparing(User::getAge, Comparator.reverseOrder()).thenComparing(User::getFraction, Comparator.reverseOrder()));


        //先以getAge升序,升序结果进行getAge降序,再进行getFraction升序,结果进行getAge降序getFraction降序
        userList.stream().sorted(Comparator.comparing(User::getAge).reversed().thenComparing(User::getFraction).reversed());

        //先以getAge升序,再进行getFraction降序
        userList.stream().sorted(Comparator.comparing(User::getAge).thenComparing(User::getFraction, Comparator.reverseOrder()));


    }







}
